package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.*;

import util.resources.ResourceManager;

/**
 * Creates IO panel. Includes a user input area as well as 
 * a program feedback area. Utilitizes property files to that
 * properties can easily be changed.  
 * 
 * @author wesleybrown
 *
 */
public class IOFrame extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextArea modelFeedback;
	private JScrollPane feedbackScroll;
	private JTextField textInput;
	
	/**
	 * Creates the IO panel. 
	 * 
	 * @param resources ResourceManager linked to property file
	 * including settings for history and variable panes.
	 * 
	 * @param controller ActionListener listening for a button click.
	 */
	public IOFrame (ResourceManager resources, ActionListener controller) {
		
		int[] frameSize = resources.getIntegerArray("ioFrameSize");
		this.setPreferredSize(new Dimension(frameSize[0], frameSize[1]));
		this.setLayout(new BorderLayout());
		
		// Create IO areas in their own panel for sizing
		JPanel LeftPanel = new JPanel();
		LeftPanel.setLayout(new GridLayout(2, 1));
		LeftPanel.setPreferredSize(new Dimension(frameSize[0], frameSize[1]));
		
		modelFeedback = new JTextArea();
		feedbackScroll = new JScrollPane(modelFeedback);
		feedbackScroll.setBorder(BorderFactory.createTitledBorder(resources.getString("modelFeedbackTitle")));
		int[] modelFeedbackSize = resources.getIntegerArray("modelFeedbackSize");
		feedbackScroll.setMinimumSize(new Dimension(modelFeedbackSize[0], modelFeedbackSize[1]));
		modelFeedback.setEditable(false);
		
		LeftPanel.add(feedbackScroll);
		
		//TODO need to figure this out later
		/*
		JTextArea textInput = new JTextArea();
		JScrollPane inputScroll = new JScrollPane(textInput);
		inputScroll.setBorder(BorderFactory.createTitledBorder(resources.getString("userInputTitle")));
		int[] userInputSize = resources.getIntegerArray("userInputSize");
		inputScroll.setMinimumSize(new Dimension(userInputSize[0], userInputSize[1]));
		LeftPanel.add(inputScroll);
		*/
		textInput = new JTextField();
		textInput.addActionListener(controller);
		LeftPanel.add(textInput);
		
		this.add(LeftPanel, BorderLayout.WEST);
		
		// Create execute button
		JButton executeButton = new JButton(resources.getString("executeButton"));
		//executeButton.addActionListener(controller);
		this.add(executeButton, BorderLayout.EAST);
		
	}
	
	/**
	 * @return The contents of feedback area.
	 */
	public JTextArea getTextArea() {
		return modelFeedback;
	}
	
	/**
	 * @return The scrollpane containing the feedback area.
	 */
	public JScrollPane getFeedbackScroller() {
		return feedbackScroll;
	}
	
	/**
	 * Automatically scrolls the feedback area to the bottom.
	 */
	public void scrollPaneToBottom() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				feedbackScroll.getVerticalScrollBar().setValue(feedbackScroll.getVerticalScrollBar().getMaximum());
			}
		});
	}
	
	public void setTextInputFocus(){
		textInput.requestFocus();
	}
}
